package com.qs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qs.mapper.GoodsMapper;
import com.qs.mapper.OrderItemMapper;
import com.qs.mapper.StoreMapper;
import com.qs.model.entity.Goods;
import com.qs.model.entity.OrderItem;
import com.qs.model.entity.Store;
import com.qs.model.entity.UserGoodsLog;
import com.qs.mapper.UserGoodsLogMapper;
import com.qs.model.enums.CommentEnum;
import com.qs.model.enums.OrderEnum;
import com.qs.model.vo.OrderInfoVo;
import com.qs.service.IUserGoodsLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户购买商品历史表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-03-31
 */
@Service
@RequiredArgsConstructor
public class UserGoodsLogServiceImpl extends ServiceImpl<UserGoodsLogMapper, UserGoodsLog> implements IUserGoodsLogService {

    private final StoreMapper storeMapper;
    private final GoodsMapper goodsMapper;
    private final OrderItemMapper orderItemMapper;

    @Override
    public List<OrderInfoVo> queryWaitDeliver(Integer userId) {
        QueryWrapper<UserGoodsLog> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("status", OrderEnum.WAIT_DELIVER.getCode());
        wrapper.orderByDesc("create_time");
        List<UserGoodsLog> userGoodsLogs = baseMapper.selectList(wrapper);
        if(!CollectionUtils.isEmpty(userGoodsLogs)) {
            return this.packageData(userGoodsLogs);
        }
        return null;
    }

    @Override
    public List<OrderInfoVo> queryWaitReceive(Integer userId) {
        QueryWrapper<UserGoodsLog> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("status", OrderEnum.WAIT_RECEIVE.getCode());
        wrapper.orderByDesc("create_time");
        List<UserGoodsLog> userGoodsLogs = baseMapper.selectList(wrapper);
        if(!CollectionUtils.isEmpty(userGoodsLogs)) {
            return this.packageData(userGoodsLogs);
        }
        return null;
    }

    @Override
    public List<OrderInfoVo> queryWaitComment(Integer userId) {
        QueryWrapper<UserGoodsLog> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("is_comment", CommentEnum.WAIT_COMMENT.getCode());
        wrapper.orderByDesc("create_time");
        List<UserGoodsLog> userGoodsLogs = baseMapper.selectList(wrapper);
        if(!CollectionUtils.isEmpty(userGoodsLogs)) {
            return this.packageData(userGoodsLogs);
        }
        return null;
    }

    @Override
    public int queryWaitDeliverCount(Integer userId) {
        QueryWrapper<UserGoodsLog> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("status", OrderEnum.WAIT_DELIVER.getCode());
        wrapper.orderByDesc("create_time");
        List<UserGoodsLog> userGoodsLogs = baseMapper.selectList(wrapper);
        if(!CollectionUtils.isEmpty(userGoodsLogs)) {
            List<OrderInfoVo> orderInfoVos = this.packageData(userGoodsLogs);
            return orderInfoVos.size();
        }
        return 0;
    }

    @Override
    public int queryWaitReceiveCount(Integer userId) {
        QueryWrapper<UserGoodsLog> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("status", OrderEnum.WAIT_RECEIVE.getCode());
        wrapper.orderByDesc("create_time");
        List<UserGoodsLog> userGoodsLogs = baseMapper.selectList(wrapper);
        if(!CollectionUtils.isEmpty(userGoodsLogs)) {
            List<OrderInfoVo> orderInfoVos = this.packageData(userGoodsLogs);
            return orderInfoVos.size();
        }
        return 0;
    }

    @Override
    public int queryWaitCommentCount(Integer userId) {
        QueryWrapper<UserGoodsLog> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("is_comment", CommentEnum.WAIT_COMMENT.getCode());
        wrapper.orderByDesc("create_time");
        List<UserGoodsLog> userGoodsLogs = baseMapper.selectList(wrapper);
        if(!CollectionUtils.isEmpty(userGoodsLogs)) {
            List<OrderInfoVo> orderInfoVos = this.packageData(userGoodsLogs);
            return orderInfoVos.size();
        }
        return 0;
    }

    /**
     * 封装数据
     * @param userGoodsLogs
     * @return
     */
    private List<OrderInfoVo> packageData(List<UserGoodsLog> userGoodsLogs) {
        List<OrderInfoVo> list = new ArrayList<>();
        userGoodsLogs.stream().forEach(item -> {
            Store store = storeMapper.selectById(item.getStoreId());
            Goods goods = goodsMapper.selectById(item.getGoodsId());
            OrderItem orderItem = orderItemMapper.selectOne(new QueryWrapper<OrderItem>()
                    .eq("order_id", item.getOrderId()));
            OrderInfoVo orderInfoVo = new OrderInfoVo();
            orderInfoVo.setOrderId(item.getOrderId());
            orderInfoVo.setGoodsId(goods.getId());
            orderInfoVo.setStoreName(store.getHomeName());
            orderInfoVo.setGoodsImage(goods.getImage());
            orderInfoVo.setGoodsName(goods.getName());
            orderInfoVo.setSale(goods.getSale());
            orderInfoVo.setPrice(goods.getPrice());
            orderInfoVo.setNumber(orderItem.getNumber());
            orderInfoVo.setTotalPrice(goods.getPrice() *  orderItem.getNumber());
            list.add(orderInfoVo);
        });
        return list;
    }
}
